<template>
  <div id="content">
    <div id="play2" :style="{backgroundImage: 'url(' + cover + ')'}"></div>
    <div class="lyc">
      <div class="lyric"  ref="lyric">
        <!-- <li class="each" v-for="(item, key, index) in currentMUsicLyric" :key="key">
          {{item}}</li> -->
        <!-- <p>{{currentTime}}</p> -->
        <!-- <p> {{duration}}</p> -->
        <p :class="{'active':currentTime > allkeys[index] && currentTime < allkeys[index+1]}" class="each" v-for="(item,name,index) in contentLyric" :key="index" >{{item}}{{scrollLRC(index)}}</p>
      </div>
    </div>

    <div class="header">
      <div>歌曲</div>
      <div>歌手</div>
      <div>时长</div>
    </div>
    <div class="hr"></div>
    <div class="box">
      <div class="cont" v-for="(item,index) in songList" :key="index" @dblclick="playMusic(item.id)">
        <div>{{ }}</div>
        <div>{{item.name}}</div>
        <div>{{item.ar[0].name}}</div>
        <div>{{item.dt}}</div>
        <div><img src="../assets/collect.png" alt="" width="15px" class="coll" @click="addMusic(item,item.id)"></div>
      </div>
    </div>
  </div>
</template>
<script>
  import axios from 'axios'
  // var songurl = ""
  export default {
    data() {
      return {
        // song_id: '',
        // author: '',
        // time: '',
        songList: [],
        songurl: "",
        lyric: "",
        cover: "",
        contentLyric: [],
        currentTime: 0,
        duration: 0,
        allkeys: []
      }
    },
    methods: {
      playMusic(id) {
        axios({
          url: 'http://music.eleuu.com/song/url',
          method: 'get',
          params: {
            id
          }
        }).then(res => {
          // console.log(res.data);
          // console.log(res.data.data[0].url);
          this.songurl = res.data.data[0].url;
          console.log(this.songurl);
          if (res.data.data[0].url != null) {
            this.$parent.$parent.$children[3].playmusic(id)
          } else {
            this.$message({
              message: '不能播放该歌曲哟',
              type: 'error'
            });
          }
          //获取时间
          this.$nextTick(() => {
            var play = this.$parent.$parent.$children[3].$refs.player
            console.log(play);
            play.addEventListener("timeupdate", () => {
              this.currentTime = play.currentTime
              // console.log(play.currentTime);
            })
            play.addEventListener("canplay", () => {
              this.duration = play.duration
              // console.log(play.duration);//总时间
            })
          })
          //歌词的
          axios({
            url: 'http://music.eleuu.com/lyric',
            method: 'get',
            params: {
              id
            }
          }).then(res => {
            // console.log(res.data.lrc.lyric);        
            this.lyric = res.data.lrc.lyric;
            // this.contentLyric.splice(0, this.contentLyric.length)
            var lrc = {}
            var lyrics = this.lyric.split("\n")
            var reg = /\[\d*:\d*(\.|:)\d*]/g;
            for (let i = 0; i < lyrics.length; i++) {
              var timeRegArr = lyrics[i].match(reg);
              if (!timeRegArr) continue;
              var time = timeRegArr[0];
              var min = parseInt(time.match(/\[\d*/i).toString().slice(1))
              var sec = parseInt(time.match(/\:\d*/i).toString().slice(1))
              var alltime = min * 60 + sec //每句时间
              var content = lyrics[i].replace(timeRegArr, "") //内容
              lrc[alltime] = content;
              // this.contentLyric.push(content)
            }
            this.contentLyric = lrc
            // console.log(this.contentLyric);
                for (var key in lrc) {
                this.allkeys.push(key)
              }
              console.log(this.allkeys);
           
          })

          axios({
            url: 'http://music.eleuu.com/song/detail',
            method: 'get',
            params: {
              ids: id
            }
          }).then(res => {
            // console.log(this.cover);
            this.cover = res.data.songs[0].al.picUrl;
            // song.cover = data.cover
          })

        })
      },
      // 添加到收藏
      addMusic(item, id) {
        let datas = {
          song_name: item.name,
          author: item.ar[0].name,
          time: item.dt,
          song_id: item.id,
          // song_url: this.songurl
        }
        // console.log(datas);
        // console.log(item);
        console.log(id);
        axios({
          url: 'http://music.eleuu.com/song/url',
          method: 'get',
          params: {
            id
          }
        }).then(res => {
          console.log(res);
          // songurl = res.data.data[0].url;
          this.song_url = res.data.data[0].url
          console.log(res.data.data[0].url);

        })

        axios.post('http://localhost:5000/music/collect', datas).then((data) => {
          console.log(data.data);
          this.$message({
            message: data.data.msg,
            type: data.data.type
          });
        })
      },
      scrollLRC(index){
              if(this.currentTime > this.allkeys[index] && this.currentTime < this.allkeys[index+1]){
                this.$refs.lyric.style.top = -30*(index-2)+"px";
              }
            }
    },
    created() {
      axios({
        url: 'http://music.eleuu.com/playlist/detail',
        method: 'get',
        params: {
          id: this.$route.query.q
        }
      }).then(res => {
        this.songList = res.data.playlist.tracks
        for (let i = 0; i < this.songList.length; i++) {
          let min = parseInt(this.songList[i].dt / 1000 / 60)
          let sec = parseInt((this.songList[i].dt / 1000) % 60)
          if (min < 10) {
            min = '0' + min
          }
          if (sec < 10) {
            sec = '0' + sec
          }
          this.songList[i].dt = min + ":" + sec
        }
        // console.log(res.data);

      })
    },


  }
</script>
<style scoped>
  #content {
    position: relative;
    left: 5%;
    color: rgb(255, 255, 255, 0.8);
    position: absolute;
    width: 1045px;
  }

  #play2 {
    position: fixed;
    left: 82.5%;
    top: 22.3%;
    width: 170px;
    height: 170px;
    background-size: 170px 170px;
  }


  .hr {
    width: 1000px;
    height: 0px;
    background: rgba(255, 255, 255, 0.3);
  }

  .header {
    position: relative;
    top: 20%;
    height: 36px;
    width: 100%;
  }

  .header div {
    position: absolute;
  }

  .header div:nth-child(1) {
    left: 0%;
  }

  .header div:nth-child(2) {
    left: 38%;
  }

  .header div:nth-child(3) {
    left: 80.5%;
  }

  .cont {
    position: relative;
    height: 50px;
  }

  .cont div:nth-child(2),
  .cont div:nth-child(3),
  .cont div:nth-child(4),
  .cont div:nth-child(5),
  .cont div:nth-child(6) {
    position: absolute;
    left: 0%;
    padding: 0px;
  }

  .cont div:nth-child(3) {
    left: 38%;
  }

  .cont div:nth-child(4) {
    left: 81%;
  }

  .cont div:nth-child(5) {
    left: 90%;
  }

  .cont div:nth-child(6) {
    left: 90%;
  }

  .box {
    height: 430px;
    overflow-y: scroll;
    display: block;
    position: relative;
    top: 15px;
  }

  ::-webkit-scrollbar {
    width: 8px;
    height: 8px;
  }

  /*定义滚动条轨道 内阴影+圆角*/
  ::-webkit-scrollbar-track {
    border-radius: 4px;
    background-color: rgba(0, 0, 0, 0.3);
  }

  /*定义滑块 内阴影+圆角*/
  ::-webkit-scrollbar-thumb {
    border-radius: 5px;
    background-color: rgb(255, 255, 255, 0.7);
  }

  .coll {
    margin: 1px 0px;
  }

  .lyc {
    width: 295px;
    height: 290px;
    position: absolute;
    top: 195px;
    left: 1085px;
    /* background-color: #333; */
    overflow-y: auto;
    color: rgb(255, 255, 255,0.5);
    font-weight: normal;
    /* padding: 5px 10px; */
    /* border: 1px solid #000; */
    /* border-left: none; */
  }
  .lyric{
    position: absolute;
    width: 290px;
    height: 180px;
    /* left: 40px; */
    top: 60px;
    text-align: center;
  }
  .active{
    color: rgb(255, 255, 255);
  }
  .each {
    height: 30px;
    /* border: 1px solid #000; */
    line-height: 30px;
    text-align: center;
    font-size: 13px;
    display: -webkit-box;
    text-overflow: ellipsis;
    -webkit-line-clamp: 1;
    overflow: hidden;
    -webkit-box-orient: vertical;

    /* color: red; */
  }
  .lyc::-webkit-scrollbar{
    width: 0px;
  }
.lyc::-webkit-scrollbar-track{
  background-color: rgb(0, 0, 0,0.1);
  
}
</style>